package quotation.web.qes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import quotation.enums.KvStoreEnum;
import quotation.exception.BusinessException;
import quotation.web.kvStore.service.KvService;
import quotation.web.personDay.ddd.entity.PersonDayEntity;
import quotation.web.personDay.mapper.PersonDayMapper;
import quotation.web.qes.ddd.dto.QesPersonDto;
import quotation.web.qes.ddd.form.QesCaculateForm;
import quotation.web.qes.service.QesService;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class QesServiceImpl implements QesService {

    @Autowired
    private KvService kvService;


    @Autowired
    private PersonDayMapper personDayMapper;
    /*
        4)中睿的成本价是 系数0.64 x R总人数 X 650 + K总证书数x1100
        5)展示给用户的报价是  系数0.64 x R总人数 X 650 + K总证书数x1100 +  K总证书数x300
     */
    @Override
    public BigDecimal caculate(QesCaculateForm form) {

        QesPersonDto qesPersonDto = this.getPersonTotal(form);
        Integer totalPerson = 0;
        Integer totalCertificate = form.getQCertNum() + form.getECertNum() + form.getSCertNum();
        qesPersonDto.setK(totalCertificate);
        if (form.getQPerson()>0) {
            LambdaQueryWrapper<PersonDayEntity> lambdaQueryWrapper = buildQueryWrapper("质量管理", form.getQPerson());
            PersonDayEntity personDayEntity = personDayMapper.selectOne(lambdaQueryWrapper);
            if (personDayEntity != null) {
                totalPerson = totalPerson+personDayEntity.getValue();
            } else {
                throw new BusinessException("未找到符合条件的记录");
            }
        }
        if (form.getEPerson()>0) {
            LambdaQueryWrapper<PersonDayEntity> lambdaQueryWrapper = buildQueryWrapper("QMS", form.getEPerson());
            PersonDayEntity personDayEntity = personDayMapper.selectOne(lambdaQueryWrapper);
            if (personDayEntity != null) {
                totalPerson += getQmsValue(personDayEntity, form.getERiskComplexity());
            } else {
                throw new BusinessException("未找到符合条件的记录");
            }
        }
        if (form.getSPerson()>0) {
            LambdaQueryWrapper<PersonDayEntity> lambdaQueryWrapper = buildQueryWrapper("QMS", form.getSPerson());
            PersonDayEntity personDayEntity = personDayMapper.selectOne(lambdaQueryWrapper);
            if (personDayEntity != null) {
                totalPerson += getQmsValue(personDayEntity, form.getSDivisionComplexity());
            } else {
                throw new BusinessException("未找到符合条件的记录");
            }
        }
        qesPersonDto.setR(totalPerson);
        //获取计算参数
        String coefficient = kvService.getOrDefault(KvStoreEnum.QES_coefficient);
        String person = kvService.getOrDefault(KvStoreEnum.QES_person);
        String cost = kvService.getOrDefault(KvStoreEnum.QES_cost);
        String cost_external = kvService.getOrDefault(KvStoreEnum.QES_cost_external);

        BigDecimal total = new BigDecimal(coefficient).multiply(new BigDecimal(qesPersonDto.getR())).multiply(new BigDecimal(person))
                .add(new BigDecimal(qesPersonDto.getK()).multiply(new BigDecimal(cost)))
                .add(new BigDecimal(qesPersonDto.getK()).multiply(new BigDecimal(cost_external)));

        return total;

    }

    private LambdaQueryWrapper<PersonDayEntity> buildQueryWrapper(String name, int personCount) {
        return new LambdaQueryWrapper<PersonDayEntity>()
                .eq(PersonDayEntity::getName, name)
                .ge(PersonDayEntity::getHigherLimit, personCount)
                .le(PersonDayEntity::getLowerLimit, personCount);
    }
    private Integer getQmsValue(PersonDayEntity personDayEntity, int riskLevel) {
        int num = 0;
        if (riskLevel == 1) {
            num = personDayEntity.getLow();
        }
        if (riskLevel == 2) {
            num = personDayEntity.getMedium();
        }
        if (riskLevel == 3) {
            num = personDayEntity.getLow();
        }
        return num;
    }
    private QesPersonDto getPersonTotal(QesCaculateForm form) {
        //todo 人天参照表
        return new QesPersonDto();
    }
}
